package com.example.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.example.config.R;
import com.example.domain.Account;
import com.example.mapper.AccountMapper;
import com.example.param.ChangePasswordParam;
import com.example.param.InsertParam;
import com.example.param.LoginParam;
import com.example.service.IAccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * 进销商账户Service业务层处理
 *
 * @author ruoyi
 * @date 2022-11-22
 */
@Service
public class AccountServiceImpl implements IAccountService {


    @Autowired
    private AccountMapper accountMapper;

    @Override
    public R login(LoginParam loginParam) {
        String userName = loginParam.getUserName();
        String password = loginParam.getPassword();
        // 封装查询
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Account::getUserName, userName);
        // 1.查询是否存在该账户
        Integer res = accountMapper.selectCount(lqw);
        if (res == 0) {
            return R.fail("不存在该账户");
        }
        // 2.查询用户名和密码是否对应
        lqw.eq(Account::getPassword, password);
        Account account = accountMapper.selectOne(lqw);
        // 正确
        if (account != null) {
            return R.ok("登录成功！", account);
        }
        // 错误
        return R.fail("登录失败，请检查用户名和密码是否正确");
    }

    @Override
    public R changePassword(ChangePasswordParam changePasswordParam) {
        System.out.println(changePasswordParam);
        Long dealerId = changePasswordParam.getDealerId();
        String oldPassword = changePasswordParam.getOldPassword();
        String newPassword = changePasswordParam.getNewPassword();
        Account account = accountMapper.selectById(dealerId);
        if (!Objects.equals(account.getPassword(), oldPassword)) {
            return R.fail("原密码错误，请重新输入");
        }
        account.setPassword(newPassword);
        int i = accountMapper.updateById(account);
        System.out.println(i);
        return R.ok("修改密码成功");
    }

    @Override
    public R insert(InsertParam insertParam) {
        Account account = new Account();
        BeanUtils.copyProperties(insertParam, account);
        int insert = accountMapper.insert(account);
        if (insert != 0) {
            return R.ok("新增成功!");
        }
        return R.fail("新增失败!");
    }

    @Override
    public R checkUserName(String username) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Account::getUserName, username);
        Account account = accountMapper.selectOne(lqw);
        if (account != null) {
            return R.fail("用户名已经存在,请重新输入");
        }
        return R.ok("用户名可用!");
    }

    @Override
    public R query(Account account) {
        LambdaQueryWrapper<Account> lqw = new LambdaQueryWrapper<>();
        lqw.like(account.getUserName() != null, Account::getUserName, account.getUserName());
        lqw.eq(account.getIsAble() != null, Account::getIsAble, account.getIsAble());
        lqw.eq(account.getRole() != null, Account::getRole, account.getRole());
        lqw.eq(account.getRealName() != null, Account::getRealName, account.getRealName());
        lqw.eq(account.getPhoneNumber() != null, Account::getPhoneNumber, account.getPhoneNumber());
        lqw.eq(account.getSex() != null, Account::getSex, account.getSex());
        List<Account> accounts = accountMapper.selectList(lqw);
        return R.ok(accounts);

    }

    @Override
    public R disable(List<Integer> ids) {
        LambdaUpdateWrapper<Account> lqw = new LambdaUpdateWrapper<>();
        lqw.in(Account::getDealerId, ids);
        lqw.set(Account::getIsAble, 0);
        int update = accountMapper.update(null, lqw);
        return R.ok("禁用成功!");
    }

    @Override
    public R able(List<Integer> ids) {
        LambdaUpdateWrapper<Account> lqw = new LambdaUpdateWrapper<>();
        lqw.in(Account::getDealerId, ids);
        lqw.set(Account::getIsAble, 1);
        int update = accountMapper.update(null, lqw);
        return R.ok("启用成功!");
    }

    @Override
    public R deleteById(List<Integer> ids) {
        accountMapper.deleteBatchIds(ids);
        return R.ok("删除成功!");
    }

    @Override
    public R getDetail(Long dealerId) {
        Account account = accountMapper.selectById(dealerId);
        return R.ok(account);
    }


}
